package com.sky.controller.common;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j

public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 清除缓存数据
     * @param pattern
     * @return
     */
 public void  clearCache(String pattern){
     Set keys = redisTemplate.keys(pattern);
     redisTemplate.delete(keys);
 }

    //分页查询菜品
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品信息，分页查询参数为：{}",dishPageQueryDTO);
       PageResult  pageResult =dishService.pageQuery(dishPageQueryDTO);
       return Result.success(pageResult);
    }

    //新增菜品
    @PostMapping
    @ApiOperation("新增菜品")
    @CachePut(value = "userCache",key = "#user.id")//Key为当前登录用户的id
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品，菜品信息为：{}",dishDTO);
        dishService.save(dishDTO);

        //清除缓存数据
        String key = "dish_"+dishDTO.getCategoryId();
        clearCache(key);
        return Result.success();
    }

    //删除菜品
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam("ids") List<Long> ids){
        log.info("删除菜品，ids为：{}",ids);
        dishService.delete(ids);
        //将所有的菜品数据从缓存中删除，所以以Dish开头的key进行删除
        clearCache("dish_*");
        return Result.success();
    }

     //按ID查询菜品
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("按ID查询菜品，id为：{}",id);
        DishVO byId = dishService.getByIdWithFlavor(id);
        return Result.success(byId);
    }

    //修改菜品
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品，菜品信息为：{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);

        //清除缓存数据
        clearCache("dish_*");
        return Result.success();
    }

    //起售停售
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启用禁用菜品，状态为：{}，id为：{}",status,id);
        dishService.startOrStop(status,id);
        //清除缓存数据
        clearCache("dish_*");
        return Result.success();
    }

    //根据分类ID查询菜品
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据分类ID查询菜品，分类ID为：{}",categoryId);
        List<Dish> List = dishService.list(categoryId);
        return Result.success(List);
    }
}
